import sys
from pathlib import Path

from PySide6.QtCore import Qt
from PySide6.QtGui import QResizeEvent
from PySide6.QtWidgets import *
from common import PdfToImgThread
from components import OpenFileDialog
from view.base import TableFrameBase, ReturnView
from qfluentwidgets import CheckBox, InfoBar, StateToolTip


class PdfToImageView(ReturnView):
    def __init__(self, parent=None):
        super().__init__(parent)

        self.frameBase = TableFrameBase()
        self.frameBase.setTitle('PDF转图片')
        self.frameBase.setHeaders(['文件名称', '完整路径', '单张图片'])

        self.stateToolTip = StateToolTip('PDF To Img', '正在PDF转图片，请稍后...', self)
        self.stateToolTip.hide()

        self.to_img_thread = PdfToImgThread(self)

        self.vBox.addWidget(self.frameBase)
        self.initEvent()

    def initEvent(self):
        self.frameBase.added.connect(self.add_event)
        self.frameBase.saved.connect(self.save_event)
        self.frameBase.started.connect(self.started_event)

        self.to_img_thread.started.connect(self.started_thread)
        self.to_img_thread.error.connect(self.error_thread)
        self.to_img_thread.finished.connect(self.finished_thread)

    def add_event(self):
        files = OpenFileDialog(self).getOpenFileNames(filter='PPT文件(*.pdf)')[0]
        for i, file in enumerate(files):
            if file in self.frameBase.getColumnValues(1):
                InfoBar.warning('提示', file + '已存在', duration=3000, parent=self)
                continue
            fp = Path(file)
            self.frameBase.addItem([fp.name, file, ''])
            checkBox = self.checkBox_frame()
            self.frameBase.setCellWidget(self.frameBase.rowCount() - 1, 2, checkBox)

    def save_event(self):
        file = OpenFileDialog(self).getExistingDirectory()
        self.frameBase.setLineText(file)

    def started_event(self):
        save = self.frameBase.lineText()
        paths = self.frameBase.getColumnValues(1)
        texts = self.frameBase.getColumnWidgetText(2)
        data = []
        for i, path in enumerate(paths):
            data.append({'path': path, 'merge': texts[i]})
        if len(data) == 0 or save is False:
            return
        print(data, save)
        # self.to_img_thread.set_value(data, save)
        # self.to_img_thread.start()

    def returnButton_event(self):
        self.frameBase.clear()
        self.returned.emit()

    def checkBox_frame(self):
        frame = QFrame()
        vBox = QVBoxLayout(frame)
        vBox.setContentsMargins(0, 0, 0, 0)
        vBox.setSpacing(0)
        vBox.setAlignment(Qt.AlignmentFlag.AlignHCenter | Qt.AlignmentFlag.AlignVCenter)
        self.checkBox = CheckBox()
        self.checkBox.setText('')
        vBox.addWidget(self.checkBox)
        return frame

    def started_thread(self):
        self.stateToolTip.show()

    def error_thread(self, msg):
        InfoBar.error('错误', msg, duration=5000, parent=self)

    def finished_thread(self):
        self.stateToolTip.hide()
        InfoBar.success('提示', '转换完成', duration=3000, parent=self)

    def resizeEvent(self, event: QResizeEvent):
        self.stateToolTip.move(event.size().width() - self.stateToolTip.width() - 20, 10)
        super().resizeEvent(event)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    win = PdfToImageView()
    win.resize(1000, 700)
    win.show()
    sys.exit(app.exec_())
